<template>
  <div class="my-input-number">
    <!-- 递减 -->
    <span
      role="button"
      @click="sub"
      class="my-input-number__decrease"
      :class="{ 'is-disabled': num === min }"
    >
      -
    </span>
    <!-- 累加 -->
    <span
      role="button"
      @click="add"
      class="my-input-number__increase"
      :class="{ 'is-disabled': num === max }"
    >
      +
    </span>
    <div class="my-input">
      <!-- 数字显示区域 -->
      <span class="my-input__inner">{{num}}</span>
    </div>
  </div>
</template>

<script>
import {ref} from 'vue'
export default {
  name: 'App',
  components: {},
  setup () {
    // 创建一个响应式的数字
    let num = ref(5)
    // 最大值 最小值
    let min = 0
    let max = 10
    // 递减
    let sub = () => {
      if (num.value > min) {
        num.value--
      }
    }
    //累加
    let add = () => {
      if (num.value < max) {
        num.value++
      }
    }
    // 暴露出去
    return { num, sub, add, min, max}
  }
}
</script>

<style>
.my-input-number {
  position: relative;
  display: inline-block;
  width: 180px;
  line-height: 38px;
}
.my-input-number span {
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
}
.my-input-number .my-input {
  display: block;
  position: relative;
  font-size: 14px;
  width: 100%;
}
.my-input-number .my-input__inner {
  -webkit-appearance: none;
  background-color: #fff;
  background-image: none;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  box-sizing: border-box;
  color: #606266;
  display: inline-block;
  font-size: inherit;
  height: 40px;
  line-height: 40px;
  outline: none;
  padding: 0 15px;
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  width: 100%;
  padding-left: 50px;
  padding-right: 50px;
  text-align: center;
}
.my-input-number .my-input-number__decrease,
.my-input-number .my-input-number__increase {
  position: absolute;
  z-index: 1;
  top: 1px;
  width: 40px;
  height: auto;
  text-align: center;
  background: #f5f7fa;
  color: #606266;
  cursor: pointer;
  font-size: 13px;
}
.my-input-number .my-input-number__decrease {
  left: 1px;
  border-radius: 4px 0 0 4px;
  border-right: 1px solid #dcdfe6;
}
.my-input-number .my-input-number__increase {
  right: 1px;
  border-radius: 0 4px 4px 0;
  border-left: 1px solid #dcdfe6;
}
.my-input-number .my-input-number__decrease.is-disabled,
.my-input-number .my-input-number__increase.is-disabled {
  color: #c0c4cc;
  cursor: not-allowed;
}
</style>
